/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @file ce_utils.c
 * @brief CE模块工具函数
 */

#include "nci_feature.h"
#include "ce_utils.h"

extern tNCI_CE_ACTS g_ce_t3t_acts;

/**
 * @brief 获取指定CE技术的API接口
 *
 * @param[in] tech CE技术类型
 *
 * @return 返回对应的CE技术API指针，失败返回NULL
 */
tNCI_CE_ACTS* ce_utils_get_acts(tNCI_CE_TECH tech) {
    tNCI_CE_ACTS *p_acts = NULL;

    NFCLOG_D("tech: 0x%02x", tech);
    switch (tech) {
    case NCI_CE_T3T:
        p_acts = &g_ce_t3t_acts;
        break;

    default:
        break;
    }
    return p_acts;
}

#if IS_FEATURE_ON(NCI_FEATURE_CE_DH)
/**
 * @brief 创建一个新的链表节点
 *
 * @return 返回新的节点指针，失败返回NULL
 */
tCE_NODE *ce_node_create() {
    tCE_NODE *newNode = (tCE_NODE*)osi_calloc(sizeof(tCE_NODE));
    if (newNode == NULL) {
        NFCLOG_E("Node create failed");
        return NULL;
    }
    newNode->next = NULL;
    return newNode;
}

/**
 * @brief 销毁链表节点
 *
 * @param[in] node 要销毁的节点指针
 */
void ce_node_destroy(tCE_NODE *node) {
    OSI_CHK_PARAM_INVALID(!node, return);
    osi_free(node);
}

/**
 * @brief 创建一个新的链表
 *
 * @return 返回新的链表指针，失败返回NULL
 */
tCE_LIST *ce_list_create() {
    tCE_LIST *list = (tCE_LIST*)osi_calloc(sizeof(tCE_LIST));
    if (list == NULL) {
        NFCLOG_E("List create failed");
        return NULL;
    }
    list->head = NULL;
    list->count = 0;
    return list;
}

/**
 * @brief 在链表头部添加节点
 *
 * @param[in] list 链表指针
 * @param[in] node 要添加的节点指针
 * @return 成功返回true，失败返回false
 */
bool ce_list_add_first(tCE_LIST *list, tCE_NODE *node) {
    OSI_CHK_PARAM_INVALID(!list, return false);
    OSI_CHK_PARAM_INVALID(!node, return false);
    node->next = list->head;
    list->head = node;
    list->count++;
    return true;
}

/**
 * @brief 在链表尾部添加节点
 *
 * @param[in] list 链表指针
 * @param[in] node 要添加的节点指针
 * @return 成功返回true，失败返回false
 */
bool ce_list_add_last(tCE_LIST *list, tCE_NODE *node) {
    OSI_CHK_PARAM_INVALID(!list, return false);
    OSI_CHK_PARAM_INVALID(!node, return false);
    if (list->head == NULL) {
        list->head = node;
    } else {
        tCE_NODE *current = list->head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = node;
    }
    list->count++;
    return true;
}

/**
 * @brief 从链表中删除节点
 *
 * @param[in] list 链表指针
 * @param[in] node 要删除的节点指针
 * @return 成功返回true，失败返回false
 */
bool ce_list_delete_node(tCE_LIST *list, tCE_NODE *node) {
    OSI_CHK_PARAM_INVALID(!list, return false);
    OSI_CHK_PARAM_INVALID(!node, return false);
    tCE_NODE *current = list->head;
    tCE_NODE *previous = NULL;

    while (current != NULL) {
        if (current == node) {
            if (previous == NULL) {
                list->head = current->next;
            } else {
                previous->next = current->next;
            }
            osi_free(current);
            list->count--;
            return true;
        }
        previous = current;
        current = current->next;
    }
    return false;
}

/**
 * @brief 销毁链表
 *
 * @param[in] list 要销毁的链表指针
 */
void ce_list_destroy(tCE_LIST *list) {
    OSI_CHK_PARAM_INVALID(!list, return);
    tCE_NODE *current = list->head;
    tCE_NODE *next;

    while (current != NULL) {
        next = current->next;
        osi_free(current);
        current = next;
    }
    osi_free(list);
}
#endif // IS_FEATURE_ON(NCI_FEATURE_CE_DH)
